" Copyright 2024 Henrique Paone
"
" This file is part of Vim-Immersive.
"
" Vim-Immersive is free software: you can redistribute it and/or modify it under the
" terms of the GNU General Public License as published by the Free Software
" Foundation, either version 3 of the License, or (at your option) any later
" version.
" 
" Vim-Immersive is distributed in the hope that it will be useful, but WITHOUT ANY
" WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
" PARTICULAR PURPOSE. See the GNU General Public License for more details.
" 
" You should have received a copy of the GNU General Public License along with
" Vim-Immersive. If not, see <https://www.gnu.org/licenses/>.

vim9script

# Immersive mode function
export def Immersive()
	# Check if writer mode is on, by fetching the bacground color of the
	# separator (15 = white) and, if it's on, turns it off
	if matchstr(execute('hi VertSplit'), 'ctermfg=\zs\d\d') == '15'
		hi VertSplit ctermfg=black
		hi NonText ctermfg=lightblue
		set laststatus=1
		set rnu
		only
		return
	endif

	# Formating
	hi VertSplit ctermfg=white # Hides split separtor
	hi NonText ctermfg=white # Hides blank line character
	set laststatus=0 # Hides status line
	set nornu # Turn off relative numbers

	var Resize_window = (new: number) => {
		var curr = winwidth(0)
		var op = (curr < new) ? '>' : '<'
		feedkeys(abs(new - curr) .. "\<C-w>" .. op, 'x')
	}
	
	# Make two read-only buffers to act as margins
	const margin_size = (winwidth(0) - &tw) / 2
	var mod = (margin_size % 2 == 0) ?  0 : 1
	
	vnew | set nomodifiable | vnew | set nomodifiable
	Resize_window(margin_size)
	wincmd l
	Resize_window(margin_size + mod - 2)
	wincmd l
	wincmd R
enddef
